package com.elinkway.infinitemovies.adapter;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.elinkway.infinitemovies.bean.PlayRecord;
import com.elinkway.infinitemovies.dao.PlayRecordDao;
import com.elinkway.infinitemovies.ui.activity.PlayHistoryActivity;
import com.elinkway.infinitemovies.utils.MoviesUtils;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class PlayHistoryListAdapter extends MyBaseAdapter {

	public List<PlayRecord> playHistroyList;
	public PlayRecordDao playRecordDao;
	public boolean isEditing = false;
	private PlayHistoryActivity playHistoryActivity;
	public ArrayList<Boolean> mChecked;
	private boolean allSelectState = false;
	public int deletedNum;
	
	private class ItemHolder {
		
		ToggleButton playhistroy_delete;
		ImageView leftImageIv;
		TextView playhistroy_title;
		TextView playhistroy_time;
		TextView playhistroy_length;
	}

	public PlayHistoryListAdapter(PlayHistoryActivity context,
			List<PlayRecord> data) { 

		super(context);
		playHistoryActivity = context;
		mChecked = new ArrayList<Boolean>();
		playHistroyList = data;
		for(int i = 0; i < playHistroyList.size(); i ++){
			
			mChecked.add(false);
			
		}
		
		playRecordDao = new PlayRecordDao(mContext);
	}
	
	public void changleAllSelectState(){
		allSelectState = !allSelectState;
	}
	public void toggle() {
		isEditing = !isEditing;
		notifyDataSetChanged();
	}
	
	
	public void refreshPlayList(List<PlayRecord> records){
		this.playHistroyList=records;
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		PlayRecord record = new PlayRecord();
		record = playHistroyList.get(position);
		ItemHolder item = new ItemHolder();
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.play_history_item_new, null);
			item = new ItemHolder();
			item.playhistroy_delete = (ToggleButton)convertView.findViewById(R.id.playhistroy_delete);
			item.leftImageIv = (ImageView) convertView.findViewById(R.id.left_image_iv);
			item.playhistroy_title = (TextView) convertView
					.findViewById(R.id.playhistroy_title);
			item.playhistroy_time = (TextView) convertView
					.findViewById(R.id.playhistroy_time);
			item.playhistroy_length = (TextView) convertView
					.findViewById(R.id.playhistroy_length);
			
			convertView.setTag(item);
		} else {
			item = (ItemHolder) convertView.getTag();
		}
		
//		String name =  TextUtils.isEmpty(record.getName())? record.getDisplayName():record.getName();

		if(!TextUtils.isEmpty(record.getPoster())){
			ImageLoader.getInstance().displayImage(record.getPoster(), item.leftImageIv);
		}else{
			item.leftImageIv.setImageResource(R.drawable.ic_launcher);
		}

		item.playhistroy_title.setText(record.getDisplayName());
		item.playhistroy_time.setText(MoviesUtils.timestamp2Recent(record.getTime(), mContext));
		item.playhistroy_length.setText(MoviesUtils.seekHistroyTimeFormat(record.getSeekHistory(), mContext));
		
		updateToggleButtonState(item, position);
		
		if (isEditing) {
		
			item.playhistroy_delete.setVisibility(View.VISIBLE);
			
		} else {
		
			item.playhistroy_delete.setVisibility(View.GONE);
			
		}
		return convertView;
	}
	
	//更新 ToggleButton的状态
	private void updateToggleButtonState(ItemHolder holder, int position) {
		if(isEditing){
			holder.playhistroy_delete.setVisibility(View.VISIBLE);
			if(position >= mChecked.size()) {
				for(int i = mChecked.size()-1; i < position; i++) {
					mChecked.add(false);
				}
			}
			if(position<mChecked.size())
				holder.playhistroy_delete.setChecked(mChecked.get(position));
			
		}else{
			
			holder.playhistroy_delete.setVisibility(View.GONE);
		
		}
	}
	
	
	
	// 选中项状态变化
	public void setSelectItemChange(int position, View view) {

		if (mChecked.get(position)) {
			deletedNum--;
			mChecked.set(position, false);
		} else {

			deletedNum++;
			mChecked.set(position, true);
		}

		playHistoryActivity.setTitle();

	}
	
	@Override
	public int getCount() {
		return playHistroyList.size();
	}
}
